﻿//创建时间: 2010-4-11
//创建作者: 张晓举
//功能说明: 管理员权限编辑
//最后修改: 2011-4-11

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Collections;
using TRPOP.CMS.UserManage;

namespace TRPOP.CMS.Web.Admin
{
    public partial class UserBasePermission_save : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_User;
        }
        #endregion

        #region 页面变量
        protected int m_id = 0;
        protected string m_logName = "不存在";
        protected AdminUserInfo mAdminUserBaseInfo = new AdminUserInfo();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }
        /// <summary>
        /// 提交保存管理权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int.TryParse(GetRequestViewState("id"), out m_id);
            mAdminUserBaseInfo = m_IAdminUser.getUserBaseInfo(m_id) as TRPOP.CMS.UserManage.AdminUserInfo;
            if (mAdminUserBaseInfo != null)
            {
                string strSelectedPermissionIds = m_interval;
                foreach (ListItem li in chkSysManageList.Items)
                {
                    if (li.Selected == true)

                        strSelectedPermissionIds += li.Value + m_interval;
                }
                foreach (ListItem li in chkPluginList.Items)
                {
                    if (li.Selected == true)

                        strSelectedPermissionIds += li.Value + m_interval;
                }
                foreach (ListItem li in chkNodeManageList.Items)
                {
                    if (li.Selected == true)

                        strSelectedPermissionIds += li.Value + m_interval;
                }
                mAdminUserBaseInfo.PermissionIds = strSelectedPermissionIds;//管理权限
                bool blnIsChangeNodeids = mAdminUserBaseInfo.NodeIds.Trim() != txtNodeids.Text.Trim();
                mAdminUserBaseInfo.NodeIds = txtNodeids.Text.Trim();         //结点菜单权限
                SubmitAlert(m_IAdminUser.SaveUserInfo(mAdminUserBaseInfo));
                if(blnIsChangeNodeids)
                    ExecuteJs("top.frames[\"leftwin\"].window.location.reload(true);");
                base.SaveLog(Plugin.LogKeys.OperObject.AdminOper, Plugin.LogKeys.OperResult.EditSubmit, m_id.ToString());
            }

            LoadBase();
        }

      
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("id"), out m_id);
            if (m_id > 0)
            {

                mAdminUserBaseInfo = m_IAdminUser.getUserBaseInfo(m_id) as TRPOP.CMS.UserManage.AdminUserInfo;
                if (mAdminUserBaseInfo != null)
                {
                    InitPageViewState("model", mAdminUserBaseInfo);
                    m_logName = mAdminUserBaseInfo.LogName;
                    txtNodeids.Text = mAdminUserBaseInfo.NodeIds;
                    CheckBoxManageListInit();
                }
                else
                {
                    string strMSG = "该用户不存在！";
                    Alert(strMSG, "UserBase_list.aspx");
                    return;
                }
            }
        }

        /// <summary>
        /// 初始化页面权限列表复选框
        /// </summary>
        public void CheckBoxManageListInit()
        {
            string strSplit = m_interval;//数据库存储权限码分割符
            //系统管理
            List<UserPermissionInfo> olist = base.GetPermissionListByParentId(1);
            chkSysManageList.Items.Clear();
            for (int i = 0; i < olist.Count; i++)
            {
                chkSysManageList.Items.Add(new ListItem(olist[i].Description, olist[i].UserRightCodeId.ToString()));
                if (mAdminUserBaseInfo.PermissionIds.Contains(strSplit + olist[i].UserRightCodeId.ToString() + strSplit))
                {
                    chkSysManageList.Items[i].Selected = true;
                }
            }

            //插件管理
            olist = base.GetPermissionListByParentId(4);
            chkPluginList.Items.Clear();
            for (int i = 0; i < olist.Count; i++)
            {
                chkPluginList.Items.Add(new ListItem(olist[i].Description, olist[i].UserRightCodeId.ToString()));
                if (mAdminUserBaseInfo.PermissionIds.Contains(strSplit + olist[i].UserRightCodeId.ToString() + strSplit))
                {
                    chkPluginList.Items[i].Selected = true;
                }

            }

            //结点管理
            olist = base.GetPermissionListByParentId(8);
            chkNodeManageList.Items.Clear();
            for (int i = 0; i < olist.Count; i++)
            {
                chkNodeManageList.Items.Add(new ListItem(olist[i].Description, olist[i].UserRightCodeId.ToString()));
                if (mAdminUserBaseInfo.PermissionIds.Contains(strSplit + olist[i].UserRightCodeId.ToString() + strSplit))
                {
                    chkNodeManageList.Items[i].Selected = true;
                }

            }
        }

        #endregion

        

    }
}